package com.financing.service.user;
import java.util.Arrays;
import java.util.Map;

import com.financing.model.invest.Bid;
import com.financing.model.invest.Deposit;
import com.financing.service.BaseService;
import com.financing.util.Money;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class DepositService extends BaseService<Bid>{

	/**
	 * 根据编码获取对象
	 * @return
	 */
	public Deposit findByCode(String code) {
		return Deposit.me.findFirst("select * from deposit where code=?", code);
	}
	
	/**
	 * 分页信息
	 * @param map 
	 * @return
	 */
	public Page<Deposit> list(int page, int size, Map<String, String> map) {
		return Deposit.me.paginate(page, size, buildSql("deposit",Arrays.asList("real_name", "amount", "user_id", "create_time", "mobile", "type", "status"),map));
	}

	public Page<Deposit> findByAgent(int page, int size, String start, String end, String agents) {
		return Deposit.me.paginate(page, size, "select *", "FROM deposit WHERE user_id in (select id from (select distinct id from user where agent_id in ("+agents+")) as tmp) AND create_time>=? AND create_time<=? ORDER BY create_time DESC ", start, end);
	}

	public double sumByParams(Map<String, String> map) {
		return Money.get(Db.findFirst(buildSql("select sum(amount) as total ","deposit",Arrays.asList("mobile", "create_time", "amount", "status"),map)).get("total"));
	}
	
	public long countByParams(Map<String, String> params) {
		Long l = (long) Db.findFirst(buildSql("select count(id) as count ","deposit",Arrays.asList("mobile", "create_time", "amount", "status"),params)).get("count");
		return l>0 ? l : 0;
	}
}
